package logic.ships.resourceShip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TriMesh;

import logic.slots.AbstractSlotCreator;

public class ResourceSlotCreator extends AbstractSlotCreator {
	
	private final String RESOURCE = "resource";
	private final String DROP = "drop";
	private final String OUT = "out";
	
	private ResourceShip ship;
	
	private HashMap<String, TriMesh> drops, outs;
	private List<ResourceSlot> slots;
	
	public ResourceSlotCreator(ResourceShip ship) {
		this.ship = ship;
		
		drops = new HashMap<String, TriMesh>();
		outs = new HashMap<String, TriMesh>();
		
		slots = new ArrayList<ResourceSlot>();
		
		init(ship.getLowQualityNode());
		createSlots();
	}
	
	private void init(Node parent) {
		for(Spatial child : parent.getChildren()) {
			if(child instanceof Node) init((Node)child);
			else if(child instanceof TriMesh) {
				TriMesh mesh = (TriMesh)child;
				String name = mesh.getName();
				String[] array = name.split("_");
				if(array.length == 1) continue;
				name = array[0].substring(0, array[0].length() - 1);
				if(!name.equals(RESOURCE)) continue;
				if(array.length == 3) {
					String type = array[2];
					if(!type.equals(DROP)) continue;
					drops.put(array[0], mesh);
				} else {
					String type = array[1];
					if(!type.equals(OUT)) continue;
					outs.put(array[0], mesh);
				}
			}
		}
	}
	
	private void createSlots() {
		for(String name : drops.keySet()) {
			TriMesh drop = drops.get(name);
			TriMesh out = outs.get(name);
			
			assert(drop != null && out != null);
			
			ResourceSlot slot = new ResourceSlot(drop, out, ship);
			ship.attachChild(slot);
			slots.add(slot);
		}
	}
	
	public List<ResourceSlot> getResourceSlots() { return slots; }
}